/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.web.utils;

import com.arisgo.cloud.core.Constant;
import com.arisgo.cloud.core.redis.UserInfo;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

/**
 * @author Arisgo
 * @since 2024/7/17
 */
public class RequestUtil {

    public static String getUsername() {
        return getUser().getUsername();
    }

    public static String getTenantName() {
        return getUser().getTenantName();
    }

    public static UserInfo getUser() {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(request, "账号未登陆");
        UserInfo user = (UserInfo) request.getRequest().getAttribute(Constant.LOGIN_USER);
        Assert.notNull(request, "账号未登陆");
        return user;
    }

    public static UserInfo getUserInfo() {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (Objects.isNull(request)) {
            return null;
        }
        return (UserInfo) request.getRequest().getAttribute(Constant.LOGIN_USER);
    }

    public static Long getUserId() {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(request, "账号未登陆");
        return Long.valueOf(request.getRequest().getParameter(UserInfo.USER_ID));
    }

    public static Long getTenantId() {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(request, "账号未登陆");
        return Long.valueOf(request.getRequest().getParameter(UserInfo.TENANT_ID));
    }

    public static Long getStaffId() {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (Objects.isNull(request)) {
            return null;
        }
        return Long.valueOf(request.getRequest().getParameter(UserInfo.STAFF_ID));
    }

    public static String getRequestPath() {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(request, "请求异常，请重试！");
        return request.getRequest().getServletPath();
    }

}
